/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.scanner.helper;

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.stream.Stream;

import javax.xml.transform.stream.StreamSource;

/**
 * @title JarHelper.java
 * @package com.hyts.scanner.helper
 * @description com.hyts.scanner.helper.JarHelper
 * @see ClassAttribute
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com
 * @date 2018年5月28日
 * @version V1.0
 */
public final class JarHelper {

	/**
	 * <p>
	 * {字段的描述}
	 * </p>
	 * 
	 * @fieldName jarClassList
	 * @fieldType List<String>
	 */
	private static List<String> jarClassList = new LinkedList<>();

	
	// 文件类型属性枚举
	public enum ClassAttribute {

		SOURCE(".java"), // java源文件
		COMPILE(".class"), // 编译以后的字节码文件
		JAR(".jar");// Jar包文件

		private String value;

		private ClassAttribute(String value) {
			this.value = value;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}
	}

	 static String testPath = "jar:file:/C:/code/jfinalwapp/WebRoot/WEB-INF/lib/jetty/jetty-server-8.1.8.jar!/com";
	/**
	 * <p>
	 * jar包的标识协议前缀
	 * </p>
	 * 
	 * @fieldName JAR_PREFIX
	 * @fieldType String
	 */
	private static final String JAR_PREFIX = "jar:";

	/**
	 * <p>
	 * jar包的标识协议后缀
	 * </p>
	 * 
	 * @fieldName JAR_SUBFIX
	 * @fieldType String
	 */
	private static final String JAR_SUBFIX = "!";

	/**
	 * <p>
	 * {字段的描述}
	 * </p>
	 * 
	 * @fieldName JAR_START_SUBFIX
	 * @fieldType String
	 */
	private static final String JAR_START_SUBFIX = "/";

	/**
	 * <b>scan</b>
	 * <p>
	 * 扫描信息操作类
	 * </p>
	 * 
	 * @param jarFilePath
	 * @exception
	 */
	public static List<String> scan(String jarFilePath) {
		URL url;
		try {
			url = new URL(jarFilePath.startsWith(JAR_PREFIX) ? jarFilePath
					: JAR_PREFIX + jarFilePath);
			String[] jarPartions = jarFilePath.split(JAR_SUBFIX);
			String basePackageScan = null;
			if (jarPartions.length == 2) {
				basePackageScan = jarPartions[1].startsWith(JAR_START_SUBFIX) ? jarPartions[1]
						.substring(1) : jarPartions[1];
			}
			JarURLConnection urlConnection = (JarURLConnection) url
					.openConnection();
			Enumeration<JarEntry> elements = urlConnection.getJarFile()
					.entries();
			while (elements.hasMoreElements()) {
				JarEntry entry = elements.nextElement();
				if (entry.getName().endsWith(ClassAttribute.COMPILE.getValue())) {
					if (basePackageScan != null) {
						if (entry.getName().startsWith(basePackageScan)) {
							jarClassList.add(entry.getName());
						}
					} else {
						jarClassList.add(entry.getName());
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jarClassList;
	}

	/**
	 * <b>scan</b>
	 * <p>
	 * 扫描信息操作类
	 * </p>
	 * 
	 * @param jarFilePath
	 * @param excludePackage
	 * @exception
	 */
	public static List<String> scan(String jarFilePath,String[] excludePackage) {
		URL url;
		try {
			url = new URL(jarFilePath.startsWith(JAR_PREFIX) ? jarFilePath
					: JAR_PREFIX + jarFilePath);
			String[] jarPartions = jarFilePath.split(JAR_SUBFIX);
			
			String basePackageScan = null;
			if (jarPartions.length == 2) {
				basePackageScan = jarPartions[1].startsWith(JAR_START_SUBFIX)?jarPartions[1].substring(1):jarPartions[1];
			}
			JarURLConnection urlConnection = (JarURLConnection) url
					.openConnection();
			Enumeration<JarEntry> elements = urlConnection.getJarFile()
					.entries();
			while (elements.hasMoreElements()) {
				JarEntry entry = elements.nextElement();
				if (entry.getName().endsWith(ClassAttribute.COMPILE.getValue())) {
					if(basePackageScan != null){
						if(!isContain(excludePackage,entry.getName()) && entry.getName().startsWith(basePackageScan)){
							jarClassList.add(entry.getName());
						}
					}else{
						jarClassList.add(entry.getName());
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jarClassList;
	}

	/**  
	 * <b>isContain</b>
	 * <p>是否包含排出包</p>
	 * @param excludePackage
	 * @param pathClass
	 * @return
	 * @exception
	 */ 
	private static boolean isContain(String[] excludePackage, String pathClass) {

		if (excludePackage == null || excludePackage.length == 0) {
			return false;
		}
		for (int i = 0; i < excludePackage.length; i++) {
			if(pathClass.startsWith(excludePackage[i])){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}
	
	/**  
	 * <b>isJarClassOrPath</b>
	 * <p>判断是否属于jar包</p>
	 * @param path
	 * @return
	 * @exception
	 */ 
	public static boolean isJarClassOrPath(String path){
		return path.contains("!") && path.contains(ClassAttribute.JAR.getValue());
	}
	
	
}
